import { Injectable } from '@nestjs/common';
import axios from 'axios';

@Injectable()
export class HttpService {
    get(url: string, params?: any): Promise<any> {
        return axios
            .get(url, {
                params,
            })
            .then(res => res.data);
    }

    post(
        url: string,
        opts?: {
            data?: any;
            params?: any;
            headers?: any;
            responseType?: any;
        },
    ): Promise<any> {
        return axios
            .post(url, opts.data, {
                headers: opts.params,
                params: opts.headers,
                responseType: opts.responseType,
            })
            .then(res => res.data);
    }

    put(
        url: string,
        opts?: {
            data?: any;
            params?: any;
            headers?: any;
        },
    ): Promise<string> {
        return axios
            .put(url, opts.data, {
                headers: opts.params,
                params: opts.headers,
            })
            .then(res => res.data);
    }
}

export let globalHttpService = new HttpService();
